﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace ZG_member
{
    public partial class WorkerAdd : Form
    {
        int id = 0;
        Worker st;
        public WorkerAdd(Worker ss)
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            st = ss;
            this.txtSalesId.ReadOnly = false;
        }

          public WorkerAdd(int ids, Worker ss)
        {
            InitializeComponent();
            id = ids;
            this.Text = "修改";
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            st = ss;
            this.txtSalesId.ReadOnly = true;
        }

          private void button1_Click(object sender, EventArgs e)
          {
              string sex = "";
              string name = this.txtName.Text;
              string money = this.txtMoney.Text;
              string birthdate = this.txtBirthDate.Text;
              string saleId = this.txtSalesId.Text;
              ZG_member.BLL.Worker sale = new ZG_member.BLL.Worker();
              ZG_member.Model.Worker saleMode = new Model.Worker();
              if (radioButton1.Checked == true)
              {
                  sex = radioButton1.Text;
              }
              if (radioButton2.Checked == true)
              {
                  sex = radioButton2.Text;
              }
              if (sex == "")
              {
                  MessageBox.Show("性别不能为空");
                  return;
              }
              if (saleId == "")
              {
                  MessageBox.Show("编号不能为空");
                  return;
              }

              if (this.Text == "修改")
              {
                  saleMode = sale.GetModel(id);
                  saleMode.Name = name;
                  saleMode.BirthDate = Convert.ToDateTime(birthdate);
                  saleMode.Sex = sex;
                  saleMode.Money = Convert.ToDouble(money);
                  sale.Update(saleMode);
                  MessageBox.Show("修改成功");
                  this.Close();
              }
              else
              {
                  try
                  {
                      saleMode.Name = name;
                      saleMode.Money = Convert.ToDouble(money);
                      saleMode.BirthDate = Convert.ToDateTime(birthdate);
                      saleMode.WorkerId = saleId;
                      saleMode.Sex = sex;
                      sale.add(saleMode);
                      MessageBox.Show("注册成功");
                  }
                  catch (Exception)
                  {

                      MessageBox.Show("此编号已注册，请重新输入！");
                      return;
                  }

              }
              st.DgvWorker.DataSource = sale.GetListWhere("").Tables[0];
              this.Close();
          }

          private void WorkerAdd_Load(object sender, EventArgs e)
          {
              timer1.Start();
              if (id != 0)
              {
                  ZG_member.BLL.Worker sale = new ZG_member.BLL.Worker();
                  ZG_member.Model.Worker saleMode = new ZG_member.Model.Worker();
                  saleMode = sale.GetModel(id);
                  this.txtName.Text = saleMode.Name;
                  this.txtSalesId.Text = saleMode.WorkerId;
                  this.txtMoney.Text = saleMode.Money.ToString();
                  this.txtBirthDate.Text = saleMode.BirthDate.ToString();
                  if (saleMode.Sex == "男")
                  {
                      this.radioButton1.Checked = true;
                  }
                  else
                  {
                      this.radioButton2.Checked = true;
                  }
              }
          }
       int  flag=0;
          private void WorkerAdd_FormClosing(object sender, FormClosingEventArgs e)
          {
              if (flag == 0)
              {
                  e.Cancel = true;
                  flag = 1;
                  this.timer2.Start();
              }
          }

          private void timer1_Tick(object sender, EventArgs e)
          {
              this.Left += 2;
              this.Opacity += 0.1;
              if (Opacity == 1)
              {
                  this.timer1.Stop();

              }
          }

          private void timer2_Tick(object sender, EventArgs e)
          {
              this.Top = this.Top + 10;
              this.Opacity = this.Opacity - 0.06;
              if (Opacity <= 0)
              {
                  this.timer2.Stop();
                  this.Close();
              }
          }

          private void button2_Click(object sender, EventArgs e)
          {
              this.Close();
          }
    }
}
